package com.dzsw.datastruct.tree.binary;

import lombok.Data;

@Data
public class Node<E> {
    /**
     * 值
     */
    private E element;
    /**
     * 左节点
     */
    private Node<E> left;
    /**
     * 右节点
     */
    private Node<E> right;
    /**
     * 父节点
     */
    private Node<E> parent;

    public Node(E element, Node<E> parent) {
        this.element = element;
        this.parent = parent;
    }

    /**
     * 是否是叶子节点
     *
     * @return
     */
    public boolean isLeaf() {
        return left == null && right == null;
    }

    /**
     * 是否是满节点
     *
     * @return
     */
    public boolean isFullNodes() {
        return left != null && right != null;
    }

    /**
     * 是否有左子树
     *
     * @return
     */
    public boolean hasLeft() {
        return left != null;
    }

    /**
     * 是否有右子树
     *
     * @return
     */
    public boolean hasRight() {
        return right != null;
    }

    /**
     * 是否是左子树
     *
     * @return
     */
    public boolean isLeftNode() {
        return this.parent.left == this;
    }

    /**
     * 是否是右子树
     *
     * @return
     */
    public boolean isRightNode() {
        return this.parent.right == this;
    }


    /**
     * 返回当前节点的兄弟节点
     * @return
     */
    public Node<E> sibling() {
        if (isLeftNode()) {
            return this.parent.right;
        }

        if (isRightNode()) {
            return this.parent.left;
        }
        return null;
    }

    /**
     * 返回当前节点的叔父节点
     * @return
     */
    public Node<E> uncle() {
        return parent.sibling();
    }

    public static <E> Node<E> createNode(E element, Node<E> parent) {
        return new Node<E>(element, parent);
    }
}
